package Interfaz;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import models.Socio;

import org.hibernate.Query;
import org.hibernate.Session;

import connection.HibernateUtil;

public class BuscarClientesDialog extends JDialog{
	
	private JTextField nombreTxt;
	private JTextField apellidosTxt;
	private JTextField DNITxt;
	private JButton buscarBtn;
	private JButton seleccionarBtn;
	private JList clientesList;
	private DefaultListModel clientesModel;
	public Session DAO;
	private Socio socioSeleccionado;
	
	public BuscarClientesDialog(JButton buscarClientesBtn){
		setLocationRelativeTo(buscarClientesBtn);
		setModal(true);
		JPanel panel= (JPanel) this.getContentPane();
		panel.setLayout(null);
		setPreferredSize(new Dimension(400, 400));
		setSize(new Dimension(400,400));
		nombreTxt=new JTextField();
		apellidosTxt=new JTextField();
		DNITxt=new JTextField();
		buscarBtn=new JButton("Buscar");
		seleccionarBtn=new JButton("Seleccionar");
		clientesModel=new DefaultListModel();
		clientesList=new JList(clientesModel);
		clientesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JLabel nombreLbl= new JLabel("Nombre: ");
		JLabel apellidosLbl= new JLabel("Apellidos: ");
		JLabel DNILbl= new JLabel("DNI: ");
		
		nombreLbl.setBounds(20, 20, 100, 25);
		apellidosLbl.setBounds(20, 40, 100, 25);
		DNILbl.setBounds(20, 60, 100, 25);
		nombreTxt.setBounds(130, 20, 150, 18);
		apellidosTxt.setBounds(130, 40, 150, 18);
		DNITxt.setBounds(130, 60, 150, 18);
		buscarBtn.setBounds(300, 20, 80, 60);
		clientesList.setBounds(20, 100, 250, 160);
		seleccionarBtn.setBounds(290, 100, 100, 20);
		
		panel.add(nombreLbl);
		panel.add(apellidosLbl);
		panel.add(DNILbl);
		panel.add(nombreTxt);
		panel.add(apellidosTxt);
		panel.add(DNITxt);
		panel.add(buscarBtn);
		panel.add(clientesList);
		panel.add(seleccionarBtn);
		
		try {
			DAO = HibernateUtil.configureSessionFactory().getCurrentSession();
			DAO.beginTransaction();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Error al conectar a la DB");
			System.exit(0);
		}
		
		buscarBtn.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				buscarClientes();
				
			}
		});
		
		seleccionarBtn.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				seleccionarCliente();
				
			}
		});
		
		
		
	}
	
	
	
	protected void seleccionarCliente() {
		Socio socio = (Socio) clientesList.getSelectedValue();
		if(socio == null){
			JOptionPane.showMessageDialog(this, "Debe seleccionar un socio");
			
		}else{
			socioSeleccionado=socio;
			dispose();
		}
		
	}

	public static Socio showBusquedaSociosDialog(JButton buscarClientesBtn){
		BuscarClientesDialog buscarClientesDialog=new BuscarClientesDialog(buscarClientesBtn);
		buscarClientesDialog.setVisible(true);
		return buscarClientesDialog.getSocioSeleccionado();
	}


	public Socio getSocioSeleccionado() {
		return socioSeleccionado;
	}



	protected void buscarClientes() {
		String nombre=nombreTxt.getText();
		String apellidos=apellidosTxt.getText();
		String dni=DNITxt.getText();
		
		if(nombre.equals("") && apellidos.equals("") && dni.equals("")){
			JOptionPane.showMessageDialog(this, "Debe indicar algùn criterio de bùsqueda.");
		}else{
			if(!dni.equals("")){
				if (checkErrorsOnDNI(dni)==false){
					JOptionPane.showMessageDialog(this, "Formato del DNI incorrecto");
				}else{
					Query query=DAO.createQuery("FROM Socio WHERE dni=:dni");
					query.setParameter("dni", dni);
					List <Socio>socios= query.list();
					clientesModel.removeAllElements();
					for (Socio socio : socios) {
						
						clientesModel.addElement(socio);
						
					}
					
				}
			}else{
				Query query=DAO.createQuery("FROM Socio WHERE nombre like :nombre and (apellido1 like :ap1 or apellido2 like :ap2)");
				query.setParameter("nombre", "%"+nombre+"%");
				query.setParameter("ap1", "%"+apellidos+"%");
				query.setParameter("ap2", "%"+apellidos+"%");
				List <Socio>socios= query.list();
				clientesModel.removeAllElements();
				for (Socio socio : socios) {
					
					clientesModel.addElement(socio);
					
				}
				
			}
		}
		
		
	}
	
	public boolean checkErrorsOnDNI(String dni) {
		boolean result = false;
		boolean result1 = false;
		boolean result2 = false;
		boolean result3 = false;
		String[] abecedario = { "a", "b", "c", "d", "e", "f", "g", "h", "i",
				"j", "k", "l", "m", "n", "�", "o", "p", "q", "r", "s", "t",
				"u", "v", "w", "x", "y", "z" };
		String[] numeros = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
		for (int a = 0; a < abecedario.length; a++) {
			if (dni.contains(abecedario[a])) {
				result1 = true;
				break;
			}
		}
		for (int a = 0; a < numeros.length; a++) {
			if (dni.contains(numeros[a])) {
				result2 = true;
				break;
			}
		}
		if (dni.length() == 9) {
			result3 = true;
		}
		if (result1 && result2 && result3)
			result = true;
		return result;
	}



	public static void main(String[] args) {
		BuscarClientesDialog b= new BuscarClientesDialog(null);
		b.setLocationRelativeTo(null);
		b.setVisible(true);
	}
	
	
}
